using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Pages_Employee_ManageVPPPayment_EditVPPPayment : System.Web.UI.Page
{
    VPPPayment payment;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["DeliverableID"] == null)
        {
            Response.Redirect("~/Pages_Employee/ManageVPPPayment/ViewVPPPayment.aspx");
        }
        if (!Page.IsPostBack)
        {
            try
            {
                int id = Convert.ToInt32(Request.QueryString["DeliverableID"]);
                payment = DALVPPPayment.GetOneVPPPaymet(id);
                if (payment.DeliverableID != 0)
                {
                    SetAllFields();
                }
                else
                {
                    Response.Redirect("~/Pages_Employee/ManageVPPPayment/ViewVPPPayment.aspx");
                }
            }
            catch
            {
                Response.Redirect("~/Errors/ConnectionError.aspx");
            }
        }
    }

    protected void SetAllFields()
    {
        try
        {
            txtDeliverableID.Text = payment.DeliverableID.ToString();
            txtAmount.Text = payment.AmountToCollect.ToString();
            txtCharge.Text = payment.Charge.ToString();
            cboIsCustomerPaid.Checked = payment.IsCustomerPaid;
        }
        catch
        {
            Response.Redirect("~/Errors/GeneralError.aspx");
        }
    }

    protected void custAmount_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            decimal amount = Convert.ToDecimal(args.Value);
            args.IsValid = (amount > 0);
        }
        catch
        {
            args.IsValid = false;
        }
    }

    protected void custCharge_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            decimal charge = Convert.ToDecimal(args.Value);
            args.IsValid = (charge > 0);
        }
        catch
        {
            args.IsValid = false;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
        if (Page.IsValid)
        {
            try
            {
                int id = Convert.ToInt32(txtDeliverableID.Text);
                decimal amount = Convert.ToDecimal(txtAmount.Text);
                decimal charge = Convert.ToDecimal(txtCharge.Text);
                bool isPaid = cboIsCustomerPaid.Checked;
                int result = DALVPPPayment.UpdateVPPPayment(id, amount, charge, isPaid);
                if (result > 0)
                {
                    lblMessage.Text = "VPP Payment edited successfully";
                }
                else
                {
                    lblMessage.Text = "Error editing VPP Payment";
                }
            }
            catch
            {
                lblMessage.Text = "Error editing VPP Payment";
            }
        }
    }
}
